/**
 * 
 */
package monopoly.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import monopoly.Player;
import monopoly.Token;

/**
 * @author manijshrestha
 * 
 *         This Frame will let the user select Name and Token. Current
 *         implementation will allow 2 users to enter their name and select a
 *         token
 */
public class PlayerSelector extends JFrame {
	private JFrame frame;
	private String[] tokenList;

	private static final long serialVersionUID = 1L;

	private JFrame parent; // to keep track of the parent window

	public PlayerSelector(JFrame parent) {
		super("Select Player");
		this.parent = parent;
		frame = new JFrame("New Game");
		tokenList = new String[] { "Boot", "Car", "Dog", "Hat", "Iron", "Ship",
				"Thimble", "WheelBarrow" };
	}

	public void init() {
		// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		parent.setVisible(false);// making the main window invisible

		frame.setSize(300, 200);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				parent.setVisible(true);
			}
		});

		JPanel titlePanel = new JPanel();
		titlePanel.setBackground(Color.WHITE);

		JLabel lblTitle = new JLabel(
				"Please type your name and select a token.");
		titlePanel.add(lblTitle);

		JPanel selectPanel = new JPanel();
		selectPanel.setLayout(new GridLayout(0, 2));
		selectPanel.setBackground(Color.WHITE);
		selectPanel.setBorder(new EmptyBorder(20, 30, 20, 30));

		JPanel btnPanel = new JPanel();
		btnPanel.setLayout(new GridLayout(0, 2));

		JLabel lblPlayer1 = new JLabel("Player 1 ");
		JLabel lblPlayer2 = new JLabel("Player 2 ");
		JLabel lblToken1 = new JLabel("Token ");
		JLabel lblToken2 = new JLabel("Token ");

		final JTextField txtPlayer1 = new JTextField();
		final JTextField txtPlayer2 = new JTextField();

		final JComboBox txtTokenSelector1 = new JComboBox(tokenList);//new JComboBox(getFilteredList(tokenList, "Car"));
		final JComboBox txtTokenSelector2 = new JComboBox(tokenList);//new JComboBox(getFilteredList(tokenList, "Boot"));
		
		JButton btnCancel = new JButton("Cancel");
		JButton btnPlay = new JButton("Play");
		
		class PlayBtnListener implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Creating a new Game GUI
				MonopolyGUI gui = new MonopolyGUI();
				
				Player player1 = new Player (txtPlayer1.getText(), 0, 1500.0, true, getToken((String)txtTokenSelector1.getSelectedItem()));
				Player player2 = new Player (txtPlayer2.getText(), 0, 1500.0, false, getToken((String)txtTokenSelector2.getSelectedItem()));
				// Starting the Game
				gui.init(player1, player2);
				frame.setVisible(false);
				
			}

			private Token getToken(String selectedItem) {
				return new Token (selectedItem, "images/token/"+selectedItem.toLowerCase()+".png");
			}
			
		}
		
		btnPlay.addActionListener(new PlayBtnListener());

		btnCancel.addActionListener(new CancelBtnListener());

		selectPanel.add(lblPlayer1);
		selectPanel.add(txtPlayer1);
		selectPanel.add(lblToken1);
		selectPanel.add(txtTokenSelector1);
		selectPanel.add(lblPlayer2);
		selectPanel.add(txtPlayer2);
		selectPanel.add(lblToken2);
		selectPanel.add(txtTokenSelector2);

		btnPanel.add(btnCancel);
		btnPanel.add(btnPlay);

		frame.add(titlePanel, BorderLayout.NORTH);
		frame.add(selectPanel, BorderLayout.CENTER);
		frame.add(btnPanel, BorderLayout.SOUTH);	

	
	}

//	private String[] getFilteredList(String[] tokenList, String excluding) {
//		int i = 0;
//		String[] tmp = new String[7];
//		for (String token : tokenList) {
//			if (!excluding.equalsIgnoreCase(token)) // assuming 2 uses Car
//			{
//				tmp[i] = token;
//				i++;
//			}
//		}
//		return tmp;
//	}

	class CancelBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			parent.setVisible(true);
			frame.setVisible(false);
		}

	}
	

}
